package com.example.blank700.spinnerdemo2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;


public class SpinnerDemo2Activity extends AppCompatActivity {
    private static final String[] countriesStr={"惠州","珠海","广州","深圳"};
    private TextView myTextView;
    private EditText myEditText;
    private Button myButton_add;
    private Button myButton_remove;
    private Spinner mySpinner;
    private ArrayAdapter adapter;
    private List allCountries;
    Animation myAnimation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_demo2);

        //构造ArrayList
        allCountries = new ArrayList();
        for(int i=0;i<countriesStr.length;++i){
            allCountries.add(countriesStr[i]);
        }
        adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,allCountries);
        //myspinner_dropdown为自定义下拉菜单样式定义在res/layout目录下
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);

        //获取对象
        myTextView=(TextView)findViewById(R.id.myTextView);
        myEditText=(EditText)findViewById(R.id.myEditText);
        myButton_add=(Button)findViewById(R.id.myButton_add);
        myButton_remove=(Button)findViewById(R.id.myButton_remove);
        mySpinner=(Spinner)findViewById(R.id.mySpinner);

        //将ArrayAdapter加入Spinner对象中
        mySpinner.setAdapter(adapter);
        //将mySpinner加入OnItemSelectedListener
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
            public void onItemSelected(AdapterView arg0,View arg1,int arg2,long arg3){
                //将所选mySpinner的值带入myTextView中
                myTextView.setText("选择了"+countriesStr[arg2]);
                //将mySpinner显示
                arg0.setVisibility(View.VISIBLE);
            }

            public void onNothingSelected(AdapterView arg0){

            }
        });

        //将myButton_add加入OnClickListener
        myButton_add.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View arg0){
                //获取到输入的字符串
                String newCountry=myEditText.getText().toString();
                for (int i=0;i<adapter.getCount();++i){
                    if(newCountry.equals(adapter.getItem(i))){
                        return;
                    }
                }
                if(!newCountry.equals("")){
                    //新增值到adapter
                    adapter.add(newCountry);
                    //获取新增值的位置
                    int position=adapter.getPosition(newCountry);
                    //将Spinner选取在新增的值的位置
                    mySpinner.setSelection(position);
                    //清空myEditText
                    myEditText.setText("");
                }
            }
        });

        //将myButton_remove加入OnClickListener
        myButton_remove.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View arg0){
                if(mySpinner.getSelectedItem()!=null){
                    //移除mySpinner的值
                    adapter.remove(mySpinner.getSelectedItem().toString());
                    //将myEditText清空
                    myEditText.setText("");
                    if (adapter.getCount()==0){
                        //将myTextView清空
                        myTextView.setText("");
                    }
                }
            }
        });

        //取得Animation定义在res/anim目录下
        myAnimation = AnimationUtils.loadAnimation(this, R.anim.my_anim);
        //将mySpinner加入OnTouchListener
        mySpinner.setOnTouchListener(new Spinner.OnTouchListener(){
            public boolean onTouch(View v,MotionEvent event){
                //将mySpinner执行Animation
                v.startAnimation(myAnimation);
                //将mySpinner隐藏
                v.setVisibility(View.INVISIBLE);
                return false;
            }
        });
        mySpinner.setOnFocusChangeListener(new Spinner.OnFocusChangeListener(){
            public void onFocusChange(View v,boolean hasFocus){

            }
        });
    }

    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.spinner_demo2, menu);
        return true;
    }
}
